package controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

import data.Data;

public class PaymentServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		response.setContentType("text/html");
		
		HttpSession session = request.getSession();
		DBObject dbObject = (DBObject) session.getAttribute("users");
		if(dbObject==null)
		{
			try {
				response.sendRedirect("/");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		String price = request.getParameter("price");
		if(price!=null&&!price.equals("")&&Integer.parseInt(price.replaceAll("[^0-9]+", ""))>=5)
		{
			DBCollection collection = Data.data("users");
			
			Date update = new Date();
			
			dbObject.put("invoice", dbObject.get("email")+"-"+update.getTime());
			collection.save(dbObject);
			String coin = String.valueOf(Integer.parseInt(price.replaceAll("[^0-9.]+", "").replaceAll("[.]{1}[0-9]+$", ""))/0.002).replaceAll("[.]{1}[0-9]+$", "");
			
			response.getWriter().println("<html>\n");
			response.getWriter().println("<head><title>Processing Payment...</title></head>\n");
			response.getWriter().println("<body onLoad=\"document.forms['paypal_form'].submit();\">\n");
			response.getWriter().println("<center><h2>Please wait, your order is being processed and you");
			response.getWriter().println(" will be redirected to the paypal website.</h2></center>\n");
			response.getWriter().println("<form method=\"post\" name=\"paypal_form\" ");
			response.getWriter().println("action=\"https://www.paypal.com/cgi-bin/webscr\">\n");
			
			response.getWriter().println("<input type=\"hidden\" name=\"business\" value=\"habogay@gmail.com\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"return\" value=\"http://hatforrent.com/manager-blog\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"cancel_return\" value=\"http://hatforrent.com/manager-blog\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"notify_url\" value=\"http://hatforrent.com/paycomplete\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"item_name\" value=\"Buy "+coin+" credit from Hatforrent\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"invoice\" value=\""+dbObject.get("email")+"-"+update.getTime()+"\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"cmd\" value=\"_xclick\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"amount\" value=\""+price+"\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"payer_email\" value=\""+dbObject.get("email")+"\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"quantity\" value=\"1\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"currency_code\" value=\"USD\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"no_shipping\" value=\"1\"/>\n");
			response.getWriter().println("<input type=\"hidden\" name=\"cpp_header_image\" value=\"http://hatforrent.com/images/logo.png\"/>\n");
		      
			response.getWriter().println("<center><br/><br/>If you are not automatically redirected to ");
			response.getWriter().println("paypal within 5 seconds...<br/><br/>\n");
			response.getWriter().println("<input type=\"submit\" value=\"Click Here\"></center>\n");
		      
			response.getWriter().println("</form>\n");
			response.getWriter().println("</body></html>\n");
		} else {
			response.getWriter().println("Error !");
		}
	}
}
